//
// Created by zhengjuping on 24-9-9.
//

#ifndef ENV_II_BT_GATTSAPPLICATION_H
#define ENV_II_BT_GATTSAPPLICATION_H

#include <esp_gap_ble_api.h>
#include <esp_gatts_api.h>
#include "stdint.h"
#include "string"
#include "memory"
#include "GattsService.h"
#include "AdvertisementData.h"
#include <list>
#include "array"



class GattsApplication {
public:
    GattsApplication(
            uint16_t applicationId,
            std::string applicationName);

    virtual ~GattsApplication() = default;

    void addService(const std::shared_ptr<GattsService> &service);

    uint16_t getApplicationId() const;

    void gapEventCallback(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
    void gattsEventCallback(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
private:
    uint16_t _applicationId;
    uint16_t _gattsIf;
    std::string _applicationName;
    std::list<std::shared_ptr<GattsService>> _services;
    std::unique_ptr<AdvertisementData> _advertisementData;
    uint8_t _advConfigDone;
    esp_ble_adv_params_t _advParams;

    void handleGapEventAdvRawDataSetComplete();
    void handleGapEventAdvRspRawDataSetComplete();
    void handleGapEventAdvStartComplete(esp_ble_gap_cb_param_t *param);
    void handleGapupdateConnParamsEvent(esp_ble_gap_cb_param_t *param);

    void handleGattsEventReg( uint16_t gattsIf );
    void handleGattsEventRead( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param  );
    void handleGattsEventWrite( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param );
    void handleGattsEventMtu( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param );
    void handleGattsEventConnect( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param );
    void handleGattsEventDisconnect( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param );
    void handleGattsEventCreateAttrTable( uint16_t gattsIf, esp_ble_gatts_cb_param_t *param );

};


#endif //ENV_II_BT_GATTSAPPLICATION_H
